<?php

include("SqlBuilder.php");
include("Logger.php");


class DatabaseType{
//Defines Database Types 
	const MySQL = 0;
	const Oracle = 1;
}

class DAL{
	private $conn;
	public $DB_NAME;
	public $DB_HOST;
	public $DB_PASS;
	public $DB_USER;
	public $DB_PLATFORM;
	
	public $SqlBuilder;
	public $Logger;
	
	function __construct($DB_NAME,$DB_HOST,$DB_PASS,$DB_USER,$DB_PLATFORM) {
		//Setup the Logger
		$this->Logger						=		new Logger("test","");
		//Setup the SQLBuilder
		$this->SqlBuilder 					=	 	new SqlBuilder();
		$this->SqlBuilder->Logger			=		$this->Logger;
		$this->SqlBuilder->DB_PLATFORM 		= 		$DB_PLATFORM;
		//Setup database Connection
		$this->DB_NAME 						=	 	$DB_NAME;
		$this->DB_HOST 						=	 	$DB_HOST;
		$this->DB_PASS 						=		$DB_PASS;
		$this->DB_USER 						= 		$DB_USER;
		$this->DB_PLATFORM 					=	 	$DB_PLATFORM;
		$this->trace						=		false;

		
	}
	
	public function DAL() {
		try{
			//Setup Database connection.
			if ($this->conn == null) {
				switch($this->DB_PLATFORM){
					case DatabaseType::MySQL:
						$this->conn = mysql_connect($this->DB_HOST, $this->DB_USER, $this->DB_PASS) or die(mysql_error());
						mysql_select_db($this->DB_NAME, $this->conn) or die(mysql_error());
						break;
					case DatabaseType::Oracle:
						$this->conn = oci_connect($this->DB_USER,$this->DB_PASS,$this->DB_HOST."/".$this->DB_NAME) or die(oci_error());
						break;
					default:
						throw new Exception('Database Platform not defined');
					break;
				}
			}else{
				return $this->conn;
			}
		} catch (Exception $e) {
			App::$Logger->Log(LogType::Error,$e->getMessage());
		}	
	}
	
	public function getInstance() {
		/*if there is no connection this will start a new one 
		or return the existing connection.*/
		if ($this->conn == null){	
			return $this->DAL();	
		}else{
			return $this->conn;
		}
	}
	
	public function getArrayFromDB($Sql){
		try{
			unset($toReturn);
			unset($row);
			unset($results);
			//returns an Array from the database.
			if ($this->conn != null) {
				switch($this->DB_PLATFORM){
					case DatabaseType::MySQL:
						$results = mysql_query($Sql) or die(mysql_error());
						$toReturn = array();
						while ($row = mysql_fetch_assoc($results)) {
							array_push($toReturn,$row);
						}
						break;
					case DatabaseType::Oracle:
						$results = oci_parse($this->conn, $Sql);
						oci_execute($results, OCI_DEFAULT);
						while (oci_fetch($results)){
							$row = oci_fetch_array($stid, OCI_ASSOC);
							array_push($toReturn,$row);
						}
						break;
					default;
						throw new Exception('Database Platform not defined');
						break;
				}
				return $toReturn;
			}else{
				throw new Exception('No Connection');
			}	
		} catch (Exception $e) {
			App::$Logger->Log(LogType::Error,$e->getMessage());
		}
		
	}
	
	public function GetDataRow($Sql){
		try{
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row;
		}catch (Exeception $e){
			App::$Logger->Log(LogType::Error,$e->getMessage());
		}
	}
	
	public function Execute($Sql){
		try{
			// Executes a function on the database.
			$pos = strpos($Sql,"SELECT");
			if($pos === false) {
				if ($this->conn != null) {
					switch($this->DB_PLATFORM){
						case DatabaseType::MySQL:
							mysql_query($Sql) or die(mysql_error());
							break;
						case DatabaseType::Oracle:
							$s = oci_parse($this->conn, $Sql);
							oci_execute($s, OCI_DEFAULT);
							break;
						default;
							throw new Exception('Database Platform not defined');
							break;
					}
				}else{
					throw new Exception('No Connection');
				}
			}else{
				throw new Exception('Cant use Execute with a SELECT statment');
			}
		} catch (Exception $e) {
			App::$Logger->Log(LogType::Error,$e->getMessage());
		}
	}
}

?>